package com.lagou.edu.utils;

import com.lagou.edu.annotation.Service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

/**
 * @author sxnan
 * @className CollectionUtils
 * @description TODO
 * @date 2020/6/6
 */
@Service
public class ConnectionUtils {

//    private ConnectionUtils(){}
//    private static ConnectionUtils connectionUtils = new ConnectionUtils();
//    public static ConnectionUtils getInstance(){
//        return connectionUtils;
//    }

    /**
     * 当前线程存储数据库连接
     */
    private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();

    public Connection getCurrentThreadConnection() throws SQLException {
        Connection con = threadLocal.get();
        if (con==null){
            con = DruidUtils.getInstance().getConnection();
            threadLocal.set(con);
        }
        return con;
    }
}
